<?php

/**
 * @author huynhtuvinh87
 * @copyright 2013
 */
class AdminController extends PermisionController {

    public $layout = '//layouts/column1';

    public function init() {
        Yii::app()->theme = 'admincp';
    }

    public function actionIndex() {
        $this->pageTitle = 'Adminstrator: Quản lý danh mục tin tức';
        $model = new Video('search');
        $model->unsetAttributes(); // clear any default values
        if (isset($_GET['Video']))
            $model->attributes = $_GET['Video'];
        $this->render('index', array(
            'model' => $model,
        ));
    }

    public function actionCreate() {
        $this->pageTitle = 'Adminstrator: Thêm mới danh mục';
        $model = new Video;
        if (isset($_POST['Video'])) {
            $model->attributes = $_POST['Video'];
            if ($model->save())
                $this->redirect('index');
        }
        $this->render('create', array('model' => $model));
    }

    public function actionUpdate($id) {
        $model = $this->loadModel($id);
        $this->pageTitle = 'Adminstrator: Cập nhật danh mục';
        $model->setScenario('update');
        if (isset($_POST['Video'])) {
            $model->attributes = $_POST['Video'];
            if ($model->save())
                $this->redirect('index');
        }
        $this->render('update', array('model' => $model));
    }

    public function actionDelete($id) {
        Yii::app()->clientScript->scriptMap = array('jquery.js' => FALSE, 'jquery.min.js' => FALSE);
        $model = $this->loadModel($id);
        $model->delete();
        if (!isset($_GET['ajax']))
            $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));
    }

    public function actionDeleteAll() {
        $autoIdAll = $_POST['autoId'];
        if (count($autoIdAll) > 0) {
            foreach ($autoIdAll as $autoId) {
                $model = $this->loadModel($autoId);
                $model->delete();
            }
        }
    }

    public function actionStatus() {
        if (!empty($_POST['id'])) {
            $model = $this->loadModel($_POST['id']);
            if ($_POST['status'] == 0)
                $model->status = 1;
            else
                $model->status = 0;
            $model->save();
        }
    }

    public function loadModel($id) {
        $model = Video::model()->findByPk($id);
        if ($model === NULL)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

}

?>